//
//  MyVC.swift
//  MD5
//
//  Created by 郑东喜 on 2016/11/25.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

class MyVC: UIViewController,MyViewheaderViewDelegate,MyViewCellDelegate,loginViewDelegate,SettingViewControllerDelegate,LoginUpViewDelegate {

    static let shared = MyVC()
    
    //头部视图
    var headerView = MyViewheaderView()
    
    //滑动视图
    var sc = UIScrollView()
    
    //登陆滑动视图
    var loginSc = UIScrollView()
    
    //视图消失时，还原导航栏
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        //隐藏导航栏
        self.navigationController?.navigationBar.isHidden = false
        
        //设置导航栏背景颜色透明
        UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
    }
    
    //视图将要出现时的操作
    override func viewWillAppear(_ animated: Bool) {

        
        //隐藏导航栏
        self.navigationController?.navigationBar.isHidden = true
        
        //设置导航栏背景颜色透明
        UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
        
        if PersonInfoModel.shared.personImg != nil {
            let headURL = PersonInfoModel.shared.personImg
            
            DispatchQueue.main.async {
                do {
                    self.headerView.headImg.image = try UIImage.init(data: Data.init(contentsOf: URL.init(string: headURL!)!))
                } catch  {
                    
                }
            }
        }
        
        //单例获取值
        if PersonInfoModel.shared.nickName != nil {
            headerView.nickName.text = PersonInfoModel.shared.nickName
            
        } else {
            if LoginModel.shared.loginStatus == "true" {
                
                if sc.frame.width != 0 {
                    return
                } else {
                    setUI()

                }
            } else {
            }

        }
//              print(PersonInfoModel.shared.nickName as Any)

    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        //判断本地是否有用户token，么有弹出登陆窗口
//        guard (localSave.object(forKey: userToken) != nil) else {
////            
//            let lov = LoginView()
//            lov.delegate = self
//            
//            let main = MainViewController()
//            
//            
//            return
//        }
        
        setUI()

    }
}



//设置登陆好的我的信息界面
extension MyVC {
    func setUI() -> Void {

        
        sc = UIScrollView(frame: CGRect(x: 0, y: -20, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height + 20))
        sc.contentSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height + 20)
        sc.showsVerticalScrollIndicator = false
        
        view.addSubview(sc)
        
        view.backgroundColor = UIColor(red: 229/255, green: 229/255, blue: 229/255, alpha: 1.0)
        
        //加载上面的view
        headerView = MyViewheaderView()
        headerView.delegate = self
        headerView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 240)
        sc.addSubview(headerView)
        
        //尾部视图
        let footerView = MyViewCell()
        footerView.frame = CGRect(x: 0, y: 260, width: UIScreen.main.bounds.width, height: 270)
        footerView.delegate = self
        sc.addSubview(footerView)
    }
    
}



// MARK:- 接收代理
extension MyVC {
    func jumpToCash() {
        self.navigationController?.pushViewController(OneDoVC(), animated: true)
    }
    
    func jumpToAccout() {
        self.navigationController?.pushViewController(AccountTableViewController(), animated: true)
    }
    
    //单机的cell
    func cellOne() {
        print("500")
//        MBManager.showBriefAlert("正在开发")
        self.navigationController?.pushViewController(JiaoYIVC(), animated: true)
    }
    func cellTwo() {
        print("501")
//        MBManager.showBriefAlert("正在开发")
        self.navigationController?.pushViewController(AwadedRecordVC(), animated: true)
    }
    
    func cellThree() {
        print("502")
//        MBManager.showBriefAlert("正在开发")
        self.navigationController?.pushViewController(ChangedRecordVC(), animated: true)
    }
    
    func cellFour() {
        print("503")
//        MBManager.showBriefAlert("正在开发")
        self.navigationController?.pushViewController(SharedVC(), animated: true)
    }
    
    func cellFive() {
        print("504")
        let setVC = SettingViewController()
        setVC.delegate = self
        self.navigationController?.pushViewController(setVC, animated: true)
    }
    
}

// MARK:- 接收代理方法
extension MyVC {
    //登陆成功
    func logSuccess() {
        
    }
    
    //关闭登陆视图后，回调本身
    func closeSelf() {
        print("-------")
        setUI()
    }
    
    //退出成功后，是在弹出登陆视图
    func back() {
    
        
        self.navigationController?.pushViewController(LoginView(), animated: true)
        
//        UIApplication.shared.keyWindow?.rootViewController = LoginView()
    }
    
    func tapToLogin() {
        print("它匹配匹配")
        let lv = LoginView()
        self.present(lv, animated: true, completion: nil)
    }
    
//    func forVC() {
//        self.navigationController?.pushViewController(ForgetViewController(), animated: true)
//    }
//    
//    func rigVC() {
//        self.navigationController?.pushViewController(RigisterViewController(), animated: true)
//    }
    
    
    func logSuc() {

    }
    
    func logSE() {
        
    }
    
    func rigSEL() {
        
    }
    
    func forSEL() {
        
    }
}
